<?php
/**
 *
 * Author:苏光盼
 * Date:2020/6/21 下午10:29
 * Class:Login.php
 * Web:https://app.admin.suanzisu.com
 * Project:通用APP后台管理系统
 *
 */

namespace App\Logic;

use App\Model\Adm;
use App\Model\AdmGroup;
use App\Model\Config;
use App\Util\Common;

use App\Util\SystemErr;
use EasySwoole\Component\Csp;
use EasySwoole\EasySwoole\Task\TaskManager;
final class Login
{
   public static function findByPhone( $phone ) : ?Adm
    {
        return Adm::create()->get(['phone'=>$phone]);
    }


    /**
     * @param array $admin
     * 登录成功后的操作
     *
     * @return array
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \Throwable
     */
    public static  function loginedSuccess( Adm $admin) : array
    {
        $Csp = new Csp();
        $Csp->add('group',function () use ($admin){
            return  self::getGroup($admin);
        })->add('menu',function () use ($admin){
            return   self::getMenu($admin);
        });
        $all = $Csp->exec();
        self::setPri($admin);
        $admin->last_login_time = time();
        $admin->update();
        return array_merge(['token' => md5($admin->id)],$all);
    }

    public static function getUserData(string $md5_uid, $field = ''){
        $r = Common::redis();
        $k = REDIS_ADMIN_USER_DATA_KEY . $md5_uid;
        $ret =  $r->get($k);
        $ttl = $r->ttl($k);
        if($ttl > 0 && $ttl < 300){
            $tm = (int)Config::create()->myGet('admin_token_life_time');
            $tm = $tm > 0 ? $tm : 300;
            $r->expire($k,$tm * 60);
        }
        if(!$field){
            return $ret;
        } else {
            if(is_string($field)){
               return isset($ret[$field]) ? $ret[$field] : null;
            } else if( is_array($field)){
                $tmp = [];
                foreach($field as $k => $v){
                    if(isset($ret[$v])){
                        array_push($tmp,$v);
                    }
                }
                return $tmp;
            }
        }
    }

    public static function setPri(Adm $admin) : void {

            try{
                Common::redis()->del(REDIS_ADMIN_USER_ROLE . md5($admin->id));
                $role = Pri::getPrisUrlByGroupId($admin->group_id);
                Common::redis()->sAdd(REDIS_ADMIN_USER_ROLE . md5($admin->id),...$role);

            } catch ( \Throwable $e){
                throw $e;
            }
    }
    /**
     * @param array $admin
     *
     * @return array
     * 生成后台前端的左侧菜单
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */

      private static function getGroup(Adm $admin) : array  {
            try {
                $token_life_time = (int)Config::create()->myGet('admin_token_life_time');
                $token_life_time = $token_life_time > 0 ? $token_life_time : 300;
                $data = $admin->hidden(['password','status'])->toArray();
                Common::redis()->setEx(REDIS_ADMIN_USER_DATA_KEY . md5($admin->id),$token_life_time * 60,
                    $data);
            }catch (\Throwable $e){
                SystemErr::write($e,'[Logic.Login.getGroup]');
            }
        return AdmGroup::create()->field(['name','login_url'])->get($admin->group_id)->toArray();
    }


      private static function getMenu(Adm $admin) : array {
        try {
           $ret = RoleMenu::findGroupMenu($admin->group_id);
            return Common::create_menu($ret);
        }  catch (\Throwable $e) {
                throw $e;
        }

    }

}